package com.ejd.web.vo.manage.user;

import java.io.IOException;

import java.util.ArrayList;
import java.util.List;


import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.component.UIComponent;
import javax.faces.component.UIViewRoot;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

import net.sf.cglib.beans.BeanCopier;

import com.ejd.service.ICustomerService;
import com.ejd.utils.EncryptUtils;
import com.ejd.utils.MessageFactory;
import com.ejd.utils.SpringFacesUtil;
import com.ejd.web.bo.impl.Customer;
import com.ejd.web.constant.EjdConstants;
import com.ejd.web.constant.SpringBeanConstants;
import com.ejd.web.vo.common.CustomerVo;

@ManagedBean(name="loginUserPage")
@RequestScoped
public class LoginUserPageBean implements java.io.Serializable {
	
	private CustomerVo customer = new CustomerVo();
	private List<String> errorMessages = new ArrayList<String>();
	private Customer customerInfo = new Customer();
	public CustomerVo getCustomer() {
		return customer;
	}

	public void setCustomer(CustomerVo customer) {
		this.customer = customer;
	}
	
	public List<String> getErrorMessages() {
		return errorMessages;
	}

	public void setErrorMessages(List<String> errorMessages) {
		this.errorMessages = errorMessages;
	}

	public Customer getCustomerInfo() {
		return customerInfo;
	}

	public void setCustomerInfo(Customer customerInfo) {
		this.customerInfo = customerInfo;
	}

	public LoginUserPageBean(){
	}

	public String loginCustomer() {
		this.getErrorMessages().clear();
		this.setCustomerInfo(null);
		ICustomerService<Customer,Long> customerService = (ICustomerService<Customer,Long>) SpringFacesUtil.getSpringBean(SpringBeanConstants.CUSTOMER_SERVICE_BEAN_NAME);
		customerService.setEntityClass(Customer.class);
		customerService.setPKClass(Long.class);
		Customer newCustomer = new Customer();
		BeanCopier copyHere = BeanCopier.create(CustomerVo.class,Customer.class,false);
		copyHere.copy(this.getCustomer(), newCustomer, null);
		Customer customer = customerService.findUniqueBy("customerId", this.getCustomer().getCustomerId());
		
		if (null == customer) {
			this.getErrorMessages().add(MessageFactory.getMessage(EjdConstants.USER_BUNDLE_NAME,"customerId.notexist.error.message", FacesMessage.SEVERITY_ERROR, this.getCustomer().getCustomerId()).getDetail());
			return null;
		} else {
			String password = (String) this.getCustomer().getPassword();
			password = EncryptUtils.encryptMD5(password);
			String realyPassword = (String) customer.getPassword();
			if (!(password.equals(realyPassword))) {
				this.getErrorMessages().add(MessageFactory.getMessage(EjdConstants.USER_BUNDLE_NAME,"password.input.wrong.error.message", FacesMessage.SEVERITY_ERROR).getDetail());
				return null;
			}
			this.setCustomerInfo(customer);
		}
		return "/index";
	}
	public String loginOffCustomer() {
		this.getErrorMessages().clear();
		this.setCustomerInfo(new Customer());
		this.setCustomer(new CustomerVo());
		return EjdConstants.GO_TO_LOGIN;
	}
}
